package com.carb.controller.admin;

import com.carb.annotation.RequiredRoles;
import com.carb.constant.AuthorityConstant;
import com.carb.dto.*;
import com.carb.entity.ItemCategory;
import com.carb.enums.CommonStatus;
import com.carb.result.PageResult;
import com.carb.result.Result;
import com.carb.service.IItemService;
import com.carb.vo.ItemStockRecordVO;
import com.carb.vo.ItemVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 物品管理 前端控制器
 * </p>
 *
 * @author XuChaoyang
 * @since 2025-01-18
 */
@RestController("adminItemController")
@RequestMapping("/admin/item")
@Tag(name = "物品管理相关接口")
@Slf4j
public class ItemController {

    @Resource
    private IItemService itemService;

    /**
     * 新增物品
     * @param itemAddDTO 新增物品信息
     */
    @RequiredRoles({AuthorityConstant.ADMIN, AuthorityConstant.STORE_MANAGER, AuthorityConstant.PURCHASER})
    @PostMapping("/add")
    @Operation(summary = "新增物品")
    public Result<String> addItem(@RequestBody ItemAddDTO itemAddDTO) {
        log.info("新增物品：{}", itemAddDTO);
        itemService.addItem(itemAddDTO);
        log.info("新增物品成功！");
        return Result.success();
    }

    /**
     * 物品分页查询
     * @param itemPageQueryDTO 查询条件
     * @return 物品分页结果
     */
    @GetMapping("/page")
    @Operation(summary = "物品分页查询")
    public Result<PageResult<ItemVO>> pageQuery(ItemPageQueryDTO itemPageQueryDTO) {
        log.info("物品分页查询：{}", itemPageQueryDTO);
        PageResult<ItemVO> pageResult = itemService.pageQuery(itemPageQueryDTO);
        log.info("物品分页查询成功！");
        return Result.success(pageResult);
    }

    /**
     * 启用/禁用物品状态
     * @param status 物品状态
     * @param id 物品id
     */
    @RequiredRoles({AuthorityConstant.ADMIN, AuthorityConstant.STORE_MANAGER, AuthorityConstant.PURCHASER})
    @PostMapping("/status/{status}")
    @Operation(summary = "启用/禁用物品状态")
    public Result<String> updateStatus(@PathVariable @Parameter(description = "物品状态") CommonStatus status,
                                       @Parameter(description = "物品id") Long id) {
        String updateStatus = status.getDesc();
        log.info("{}物品状态：{},{}", updateStatus, id, status);
        itemService.updateStatus(id, status);
        log.info("{}物品状态成功！", updateStatus);
        return Result.success();
    }

    /**
     * 根据物品id查询物品信息
     * @param id 物品id
     * @return 查询结果
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据物品id查询物品信息")
    public Result<ItemVO> getItemById(@PathVariable @Parameter(description = "物品id") Long id) {
        log.info("根据物品id查询物品信息，物品id：{}", id);
        ItemVO itemVO = itemService.getItemById(id);
        log.info("根据物品id查询物品信息成功！");
        return Result.success(itemVO);
    }

    /**
     * 修改物品信息
     * @param itemUpdateDTO 修改的物品信息
     */
    @RequiredRoles({AuthorityConstant.ADMIN, AuthorityConstant.STORE_MANAGER, AuthorityConstant.PURCHASER})
    @PutMapping
    @Operation(summary = "修改物品信息")
    public Result<String> updateItem(@RequestBody ItemUpdateDTO itemUpdateDTO) {
        log.info("修改物品信息：{}", itemUpdateDTO);
        itemService.updateItem(itemUpdateDTO);
        log.info("修改物品信息成功！");
        return Result.success();
    }

    /**
     * 删除物品
     * @param id 物品id
     */
    @RequiredRoles({AuthorityConstant.ADMIN, AuthorityConstant.STORE_MANAGER, AuthorityConstant.PURCHASER})
    @DeleteMapping("/{id}")
    @Operation(summary = "删除物品")
    public Result<String> deleteItem(@PathVariable @Parameter(description = "物品id") Long id) {
        log.info("删除物品，物品id：{}", id);
        itemService.deleteItem(id);
        log.info("删除物品成功！");
        return Result.success();
    }

    /**
     * 新增物品类别
     * @param itemCategoryName 新增物品类别名称
     */
    @RequiredRoles({AuthorityConstant.ADMIN, AuthorityConstant.STORE_MANAGER, AuthorityConstant.PURCHASER})
    @PostMapping("/category/add")
    @Operation(summary = "新增物品类别")
    public Result<String> addItemCategory(@RequestParam @Parameter(description = "物品类别名称", required = true) String itemCategoryName) {
        log.info("新增物品类别：{}", itemCategoryName);
        itemService.addItemCategory(itemCategoryName);
        log.info("新增物品类别成功！");
        return Result.success();
    }

    /**
     * 物品类别分页查询
     * @param itemCategoryPageQueryDTO 查询条件
     * @return 物品类别分页结果
     */
    @GetMapping("/category/page")
    @Operation(summary = "物品类别分页查询")
    public Result<PageResult<ItemCategory>> pageQueryItemCategory(ItemCategoryPageQueryDTO itemCategoryPageQueryDTO) {
        log.info("物品类别分页查询：{}", itemCategoryPageQueryDTO);
        PageResult<ItemCategory> pageResult = itemService.pageQueryItemCategory(itemCategoryPageQueryDTO);
        log.info("物品类别分页查询成功！");
        return Result.success(pageResult);
    }

    /**
     * 启用/禁用物品类别状态
     * @param status 物品类别状态
     * @param id 物品类别id
     */
    @RequiredRoles({AuthorityConstant.ADMIN, AuthorityConstant.STORE_MANAGER, AuthorityConstant.PURCHASER})
    @PostMapping("/category/status/{status}")
    @Operation(summary = "启用/禁用物品类别状态")
    public Result<String> updateItemCategoryStatus(@PathVariable @Parameter(description = "物品类别状态") CommonStatus status,
                                                   @Parameter(description = "物品类别id") Long id) {
        String updateStatus = status.getDesc();
        log.info("{}物品类别状态：{},{}", updateStatus, id, status);
        itemService.updateItemCategoryStatus(id, status);
        log.info("{}物品类别状态成功！", updateStatus);
        return Result.success();
    }

    /**
     * 根据物品类别id查询物品类别信息
     * @param id 物品类别id
     * @return 查询结果
     */
    @GetMapping("/category/{id}")
    @Operation(summary = "根据物品类别id查询物品类别信息")
    public Result<ItemCategory> getItemCategoryById(@PathVariable @Parameter(description = "物品类别id") Long id) {
        log.info("根据物品类别id查询物品类别信息，物品类别id：{}", id);
        ItemCategory itemCategory = itemService.getItemCategoryById(id);
        log.info("根据物品类别id查询物品类别信息成功！");
        return Result.success(itemCategory);
    }

    /**
     * 修改物品类别信息
     * @param itemCategoryUpdateDTO 修改的物品类别信息
     */
    @RequiredRoles({AuthorityConstant.ADMIN, AuthorityConstant.STORE_MANAGER, AuthorityConstant.PURCHASER})
    @PutMapping("/category")
    @Operation(summary = "修改物品类别信息")
    public Result<String> updateItemCategory(@RequestBody ItemCategoryUpdateDTO itemCategoryUpdateDTO) {
        log.info("修改物品类别信息：{}", itemCategoryUpdateDTO);
        itemService.updateItemCategory(itemCategoryUpdateDTO);
        log.info("修改物品类别信息成功！");
        return Result.success();
    }

    /**
     * 删除物品类别
     * @param id 物品类别id
     */
    @RequiredRoles({AuthorityConstant.ADMIN, AuthorityConstant.STORE_MANAGER, AuthorityConstant.PURCHASER})
    @DeleteMapping("/category/{id}")
    @Operation(summary = "删除物品类别")
    public Result<String> deleteItemCategory(@PathVariable @Parameter(description = "物品类别id") Long id) {
        log.info("删除物品类别，物品类别id：{}", id);
        itemService.deleteItemCategory(id);
        log.info("删除物品类别成功！");
        return Result.success();
    }

    /**
     * 查询所有启用的物品类别
     * @return 启用的物品类别列表
     */
    @GetMapping("/category/list")
    @Operation(summary = "查询所有启用的物品类别")
    public Result<List<ItemCategory>> listItemCategory() {
        log.info("查询所有启用的物品类别");
        List<ItemCategory> itemCategoryList = itemService.listItemCategory();
        log.info("查询所有启用的物品类别成功！");
        return Result.success(itemCategoryList);
    }

    /**
     * 物品入库
     * @param itemStockUpdateDTOList 物品入库信息列表
     */
    @RequiredRoles({AuthorityConstant.ADMIN, AuthorityConstant.STORE_MANAGER, AuthorityConstant.PURCHASER})
    @PostMapping("/stock/add")
    @Operation(summary = "物品入库")
    public Result<String> addItemStock(@RequestBody List<ItemStockUpdateDTO> itemStockUpdateDTOList) {
        log.info("物品入库：{}", itemStockUpdateDTOList);
        itemService.addItemStock(itemStockUpdateDTOList);
        log.info("物品入库成功！");
        return Result.success();
    }

    /**
     * 物品出库
     * @param itemStockUpdateDTOList 物品出库信息列表
     */
    @RequiredRoles({AuthorityConstant.ADMIN, AuthorityConstant.STORE_MANAGER, AuthorityConstant.PURCHASER})
    @PostMapping("/stock/reduce")
    @Operation(summary = "物品出库")
    public Result<String> reduceItemStock(@RequestBody List<ItemStockUpdateDTO> itemStockUpdateDTOList) {
        log.info("物品出库：{}", itemStockUpdateDTOList);
        itemService.reduceItemStock(itemStockUpdateDTOList);
        log.info("物品出库成功！");
        return Result.success();
    }

    /**
     * 物品库存记录分页查询
     * @param itemStockRecordPageQueryDTO 查询条件
     * @return 物品库存记录分页结果
     */
    @GetMapping("/stock/page")
    @Operation(summary = "物品库存记录分页查询")
    public Result<PageResult<ItemStockRecordVO>> pageQueryItemStockRecord(ItemStockRecordPageQueryDTO itemStockRecordPageQueryDTO) {
        log.info("物品库存记录分页查询：{}", itemStockRecordPageQueryDTO);
        PageResult<ItemStockRecordVO> pageResult = itemService.pageQueryItemStockRecord(itemStockRecordPageQueryDTO);
        log.info("物品库存记录分页查询成功！");
        return Result.success(pageResult);
    }

}
